package com.example.nfcaccesssystem;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class NewDoorActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    setContentView(R.layout.activity_new_door);
	    
	    ListView userListView = (ListView)findViewById(R.id.availableUsersListView);
	    
	    userListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
	    
		final ArrayAdapter<User> adapter = new ArrayAdapter<User>(this, android.R.layout.simple_list_item_multiple_choice, Controller.getInstance().userList);
		
		userListView.setAdapter(adapter);
		
		Controller.getInstance().getUsers();

	}
	
	public void createDoor(View v){
		EditText doorInfoText = (EditText)findViewById(R.id.doorInfoText);
		
		String newDoorInfo = doorInfoText.getText().toString();
		
		ListView userListView = (ListView)findViewById(R.id.availableUsersListView);
		
		ArrayList<User> userList = new ArrayList<User>();
		
		SparseBooleanArray selectedPos = userListView.getCheckedItemPositions();
		
		for (int j = 0; j < userListView.getAdapter().getCount(); j++){
	    	if (selectedPos.get(j)) {
	    		userList.add((User) userListView.getAdapter().getItem(j));
	    	}
	    }
		
		Controller.getInstance().createDoor(this, newDoorInfo, userList.toArray(new User[0]));
	}
	
	public void cancelNewDoor(View v){
		
	}
	
	public void setNewDoor(boolean response) {
		if (response) {
			Intent i = new Intent(this, MainActivity.class);
			startActivity(i);
		}
	}

}
